package com.test.learnkotlin.ui.activity


import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import com.test.learnkotlin.*
import com.test.learnkotlin.adapter.CommonAdapter
import com.test.learnkotlin.base.BaseActivity
import com.test.learnkotlin.databinding.ActivityLoginBinding
import com.test.learnkotlin.utils.LogUitls
import com.test.learnkotlin.utils.ToastUtil
import com.test.learnkotlin.viewmodel.LoginViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel

/*
    Kotlin 注解处理 https://www.kotlincn.net/docs/reference/kapt.html

    Android Studio Kotlin databinding: Unresolved reference on BR       https://stackoverflow.com/questions/57983508/android-studio-kotlin-databinding-unresolved-reference-on-br

    apply plugin: 'kotlin-kapt' in app build.gradle.

    https://blog.csdn.net/weixin_40929353/article/details/102911137


    Retrofit+协程，网络请求封装实战
    https://blog.fintopia.tech/6119e0152078082a378ec5f4/


    javap使用总结
    https://blog.csdn.net/Weixiaohuai/article/details/119764745

    ./app/src/main/java/com/test/learnkotlin/ui/activity LoginActivity.kt

    terminal里切换路径 cd ./app/src/main/java/com/test/learnkotlin/ui/activity


    terminal里切换路径 cd ./app/build/tmp/kotlin-classes/debug/com/test/learnkotlin/ui/activity  LoginActivity.class

    javap -v LoginActivity.class


 */

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    var et_mobile: EditText? = null
    var et_sms_code: EditText? = null
    lateinit var stu: User
            var timeDownScope: CoroutineScope? = null

    override fun getContentLayoutId(): Int = R.layout.activity_login

    override fun variableId(): Int = BR.mLoginViewModel

    override fun initView() {
        super.initView()
        if (stu != null) {
            stu.name
        }

        /*
         et_mobile.let {
             it!!.text // 这里为了测试 GlobalCrashHandlerKT 全局捕获到的异常信息
             LogUitls.i("et_mobile 空指针异常")
         }
         */


        et_mobile = mDataBinding.etMobile
        et_sms_code = mDataBinding.etSmscode

        startCountDown()

    }

    fun startCountDown() {
        countDown(time = 10, start = {
            timeDownScope = it
            logI("倒计时开始...")
        }, end = {
            logI("倒计时结束.")

        }, next = {
            logI("当前倒计时：$it")

            if (it == 50){
                timeDownScope?.cancel()
            }
        })
    }

    fun startCountDown2() {
        var timeDownScope: CoroutineScope? = null
        countDown( 60,{
            timeDownScope = it
            logI("倒计时开始...")
        }, {
            logI("倒计时结束.")

        }, {
            logI("当前倒计时：$it")

            if (it == 50){
                timeDownScope?.cancel()
            }
        })
    }

    override fun initData() {
        super.initData()
        mViewModel.commonAdapter.set(CommonAdapter())
    }

    override fun initEvent() {
        super.initEvent()
        getViewModel().smsCodeLiveEvent.observe(this, {
            var mobile: String = et_mobile?.text.toString()
            if (mobile.isNullOrEmpty() || mobile.length != 10) {
                ToastUtil.show("请输入正确的手机号")
                return@observe
            }

            mViewModel.smscode2("REGISTER", mobile)

        })

//        findViewById<TextView>(R.id.tv_sms_code).setOnClickListener {
//            var mobile: String = et_mobile?.text.toString()
//            if (mobile.isNullOrEmpty() || mobile.length != 10) {
//                ToastUtil.show("请输入正确的手机号")
//                return@setOnClickListener
//            }
//
//            getViewModel()?.smsCode("REGISTER", mobile)
//
//        }

        findViewById<TextView>(R.id.tv_login).setOnClickListener {
            var mobile: String = et_mobile?.text.toString()

            if (mobile.isNullOrEmpty() || mobile.length != 10) {
                ToastUtil.show("请输入正确的手机号")
                return@setOnClickListener
            }

            var smsCode: String = et_sms_code?.text.toString()

            if (smsCode.isNullOrEmpty() || smsCode.length != 4) {
                ToastUtil.show("请输入正确的验证码")
                return@setOnClickListener
            }

            getViewModel().login(mobile, smsCode)

        }

    }
}